package de.jmda.mview.fx.node.typeshape.relation;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Point2D;

import javax.lang.model.element.VariableElement;

import de.jmda.fx.node.ArrowOrthogonal;
import de.jmda.fx.node.ArrowOrthogonal.Orientation;
import de.jmda.mview.fx.node.typeshape.FieldView;
import de.jmda.util.gui.awt.graphics.RelationEndpoint.Style;

public class RelationEndpointSourceFieldBased
		extends RelationEndpointSource
{
//	private final static Logger LOGGER =
//      Logger.getLogger(RelationEndpointSourceFieldBased.class);

	/**
	 * source field, injected into constructor
	 * TODO get {@link #field} from {@link #fieldView} somehow
	 */
	private VariableElement field;

	/**
	 * source field view, injected into constructor
	 * TODO get {@link #field} from {@link #fieldView} somehow
	 */
	private FieldView fieldView;
	
	public RelationEndpointSourceFieldBased(
			VariableElement field, final FieldView fieldView)
	{
		super();

		// store injected values
		this.field = field;
		this.fieldView = fieldView;

		// initially locate arrow at the right side of fieldview with left direction
		arrow =
				new ArrowOrthogonal(
						fieldView.anchorRightProperty().get(),
						10,
						Style.NONE,
						false,
						10,
						Orientation.LEFT);

		this.fieldView.anchorRightProperty().addListener
		(
				new ChangeListener<Point2D>()
				{
					@Override
          public void changed(
          		ObservableValue<? extends Point2D> source,
          		Point2D pointOld, Point2D pointNew)
          {
						Point2D anchorRightOnTypeshape =
								fieldView.getAnchorRightOnTypeshape();

						arrow.setLayoutX(anchorRightOnTypeshape.getX());
						arrow.setLayoutY(anchorRightOnTypeshape.getY());

//						LOGGER.debug(arrow.coordinatesAsString());
          }
				}
		);
  }
}